﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Controls;
using Caliburn.Micro;
using Microsoft.Phone.Controls;
using Ninject;
using Spotted.Controls;

namespace Spotted.Core
{
    public class SpottedBootstrapper : PhoneBootstrapper
    {
        public IKernel NinjectContainer { get; private set; }

        protected override void Configure()
        {
            // set up the DI cointainer
            NinjectContainer = new StandardKernel(new SpottedNinjectModule(RootFrame));

            // Add Custom Contentions
            AddCustomConventions();
        }

        protected override object GetInstance(Type service, string key)
        {
            return service == null ? null : NinjectContainer.Get(service);
        }

        protected override IEnumerable<object> GetAllInstances(Type service)
        {
            return NinjectContainer.GetAll(service);
        }

        protected override void BuildUp(object instance)
        {
            NinjectContainer.Inject(instance);
        }

        static void AddCustomConventions()
        {
            ConventionManager.AddElementConvention<ListPicker>(ListPicker.SelectedItemProperty, "SelectedItem", "SelectionChanged");
            ConventionManager.AddElementConvention<SpottedImage>(SpottedImage.ImageDataProperty, "ImageData",
                                                                 string.Empty);

            /*ConventionManager.AddElementConvention<Pivot>(Pivot.ItemsSourceProperty, "SelectedItem", "SelectionChanged").ApplyBinding =
                (viewModelType, path, property, element, convention) => {
                    if (ConventionManager
                        .GetElementConvention(typeof(ItemsControl))
                        .ApplyBinding(viewModelType, path, property, element, convention))
                    {
                        ConventionManager
                            .ConfigureSelectedItem(element, Pivot.SelectedItemProperty, viewModelType, path);
                        ConventionManager
                            .ApplyHeaderTemplate(element, Pivot.HeaderTemplateProperty, null, viewModelType);
                        return true;
                    }

                    return false;
                };

            ConventionManager.AddElementConvention<Panorama>(Panorama.ItemsSourceProperty, "SelectedItem", "SelectionChanged").ApplyBinding =
                (viewModelType, path, property, element, convention) => {
                    if (ConventionManager
                        .GetElementConvention(typeof(ItemsControl))
                        .ApplyBinding(viewModelType, path, property, element, convention))
                    {
                        ConventionManager
                            .ConfigureSelectedItem(element, Panorama.SelectedItemProperty, viewModelType, path);
                        ConventionManager
                            .ApplyHeaderTemplate(element, Panorama.HeaderTemplateProperty, null, viewModelType);
                        return true;
                    }

                    return false;
                };*/
        }
    }
}
